package com.shui_shui.compass;

import com.shui_shui.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;

public class CompassView extends View{
	
	private int bearing;
	
	private Paint markerPaint;
	private Paint textPaint;
	private Paint circlePaint;
	private String north;
	private String east;
	private String west;
	private String south;
	private Resources r;
	private int textHeigth;
	
	public CompassView(Context context) {
		super(context);
		initCompassView();
	}
	
	public CompassView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initCompassView();
	}

	public CompassView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initCompassView();
	}
	
	public float getBearing() {
		return bearing;
	}

	public void setBearing(float bearing) {
		this.bearing = (int)bearing;
		sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
	}

	protected void initCompassView(){
		setFocusable(true);
		
		r = this.getResources();
		
		circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint.setStrokeWidth(2);
		circlePaint.setAntiAlias(true);
		
		north = r.getString(R.string.north);
		east = r.getString(R.string.east);
		south = r.getString(R.string.south);
		west = r.getString(R.string.west);
		
		textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setColor(r.getColor(R.color.text_color));

		textHeigth = (int)textPaint.measureText("yY");
		
		markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		markerPaint.setColor(r.getColor(R.color.marker_color));
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		int px = getMeasuredWidth()/2;
		int py = getMeasuredHeight()/2;
		
		int radius = Math.min(px, py);
		radius--;
		
		//InCricle
		circlePaint.setColor(r.getColor(R.color.background_color));
		circlePaint.setAlpha(150);
		circlePaint.setStyle(Paint.Style.FILL);
		canvas.drawCircle(px, py, radius, circlePaint);
		canvas.save();
		
		//OutCricle
		circlePaint.setColor(r.getColor(R.color.marker_color));
		circlePaint.setAlpha(255);
		circlePaint.setStyle(Paint.Style.STROKE);
		canvas.drawCircle(px, py, radius, circlePaint);
		canvas.save();
		
		//Radius Center Text
		int bear = bearing;
		if(bearing<0)
			bear = bear+360;
		String bearingText = String.valueOf(bear);
		int bearingWidth=radius-(int)textPaint.measureText(bearingText);
		textPaint.setTextSize(32);
		canvas.drawText(bear+" °", bearingWidth, radius+textHeigth, textPaint);
		canvas.save();
		
		//Maker Center
		markerPaint.setStrokeWidth(2);
		canvas.drawLine(px, 0, px, 15, markerPaint);
		canvas.save();
		
		markerPaint.setStrokeWidth(1);
		canvas.rotate(-bearing, px, py);
		textPaint.setTextSize(16);
		int textWidth = (int)textPaint.measureText("W");
		int cardinalX = px-textWidth/2;
		int cardinalY = py-radius+textHeigth;
		
		
		
		for(int i = 0;i < 24; i++){
			canvas.drawLine(px, py-radius, px, py-radius+10, markerPaint);
			
			canvas.save();
			canvas.translate(0, textHeigth);
			
			if(i%6 == 0){
				String dirString = "";
				switch (i) {
				case (0):{
					dirString = north;
					int arrowY = 2*textHeigth;
					canvas.drawLine(px, arrowY, px-5, 3*textHeigth, markerPaint);
					canvas.drawLine(px, arrowY, px+5, 3*textHeigth, markerPaint);
					break;
				}
				case (6):{
					dirString = east;
					break;
				}
				case (12):{
					dirString = south;
					break;
				}
				case (18):{
					dirString = west;
					break;
				}				
				default:
					break;
				}
				
				canvas.drawText(dirString, cardinalX, cardinalY, textPaint);
			}else if(i%3 == 0){
				String angle = String.valueOf(i*15);
				float angleTextWidth = textPaint.measureText(angle);
				
				int angleTextX = (int)(px-angleTextWidth/2);
				int angleTextY = py-radius+textHeigth;
				canvas.drawText(angle, angleTextX, angleTextY, textPaint);
			}
			canvas.restore();
			
			canvas.rotate(15,px,py);			
		}
		canvas.restore();		
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		
		int measureWidth = measure(widthMeasureSpec);
		int min = (int)(measureWidth*0.8);
		setMeasuredDimension(min+2,min+2);
		
	}
	
	private int measure(int measureSpec){
		int result = 0;
		// Decode the measurement specifications.
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		if (specMode == MeasureSpec.UNSPECIFIED) {
			// Return a default size of 200 if no bounds are specified.
			result = 200;
		} else {
			// As you want to fill the available space
			// always return the full available bounds.
			result = specSize;
		}
		return result;
		
	}	
	
}
